package com.jzo2o.market.handler;

import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.task.SyncMessageTask;
import com.jzo2o.redis.sync.SyncManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class XxlJobHandler {

    @Resource
    private SyncManager syncManager;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponService couponService;

    /**
     * 活动状态修改，
     * 1.活动进行中状态修改
     * 2.活动已失效状态修改
     * 1分钟一次
     */
    @XxlJob("updateActivityStatus")
    public void updateActivitySatus(){
        //对于待生效的活动：到达发放开始时间状态改为“进行中”。
        List<Activity> list = activityService.lambdaQuery()
                .eq(Activity::getStatus, ActivityStatusEnum.NO_DISTRIBUTE.getStatus())
                .last("and distribute_start_time<=now()")
                .list();
        if(CollUtils.isNotEmpty(list)){
            for (Activity activity : list) {
               activityService.lambdaUpdate()
                       .set(Activity::getStatus,ActivityStatusEnum.DISTRIBUTING.getStatus())
                       .eq(Activity::getId,activity.getId())
                       .update();
            }
        }
        //对于待生效及进行中的活动：到达发放结束时间状态改为“已失效”
        List<Activity> list1 = activityService.lambdaQuery()
                .ne(Activity::getStatus, ActivityStatusEnum.LOSE_EFFICACY.getStatus())
                .last("and distribute_end_time<=now()")
                .list();
        if(CollUtils.isNotEmpty(list1)){
            for (Activity activity : list1) {
                activityService.lambdaUpdate()
                        .set(Activity::getStatus,ActivityStatusEnum.LOSE_EFFICACY.getStatus())
                        .eq(Activity::getId,activity.getId())
                        .update();
            }
        }
    }

    /**
     * 已领取优惠券自动过期任务
     */
    @XxlJob("processExpireCoupon")
    public void processExpireCoupon() {
      //用户领取的优惠券如果到达有效期仍然没有使用自动改为“已失效”
        List<Coupon> list = couponService.lambdaQuery()
                .eq(Coupon::getStatus, 1)
                .last("and validity_time<=now()")
                .list();
        if(CollUtils.isEmpty(list)){
            return;
        }
        for (Coupon coupon : list) {
            couponService.lambdaUpdate()
                    .set(Coupon::getStatus,3)
                    .eq(Coupon::getId,coupon.getId())
                    .update();
        }
    }

    @XxlJob("activityPreheat")
    public void activityPreHeat() {
        log.info("优惠券活动定时预热...");
        try {
            activityService.preHeat();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Autowired
    private SyncMessageTask syncMessageTask;
    @XxlJob("seizeCouponSyncJob")
    public void seizeCouponSyncJob() {
        log.info("抢券结果同步任务...");
        try {
            for (long index = 0; index < 10; index++) {
                syncMessageTask.batchHandeSyncMessage(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
